//
//  MJTComposeToolBar.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/19.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
//定义枚举
enum MJTComposeToolBarButtonType: Int {
    //图片
    case Picture = 0
    // @
    case Mention = 1
    //# 话题
    case Trend = 2
    //表情
    case Emoticon = 3
    // 加号
    case Add = 4
}
class MJTComposeToolBar: UIStackView {
    //  点击toolbar按钮回调的闭包
    var selectedToolBarButtonClosure: ((type: MJTComposeToolBarButtonType)->())?

    //  toolbar表情子按钮
    var emotionButton: UIButton?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {

        addChildButton("compose_toolbar_picture",type: .Picture)
        addChildButton("compose_mentionbutton_background",type: .Mention)
        addChildButton("compose_trendbutton_background",type: .Trend)
       emotionButton = addChildButton("compose_emoticonbutton_background",type: .Emoticon)
        addChildButton("compose_add_background",type: .Add)
    }
    private func addChildButton(imageName: String, type: MJTComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        //设置布局
        axis = .Horizontal
        //设置填充方式
        distribution = .FillEqually
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        //取消高亮
        button.adjustsImageWhenHighlighted = false
        addArrangedSubview(button)
        return button
    }
    //MARK: -- 点击事件
    @objc private func buttonAction(button: UIButton) {
        let type = MJTComposeToolBarButtonType(rawValue: button.tag)!
        button.highlighted = true
        selectedToolBarButtonClosure?(type: type)
    }
    // 根据是否是表情键盘显示不同的图标
    func showEmoticonIcon(isEmoticon: Bool){
        if isEmoticon {
            emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emotionButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)

        } else {
            emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emotionButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
    }
}
